import $util from './util.js'

/**
 * @description uni-app全局组件
 * 
 * */
export default {
	
	/**
	 * 模态弹窗
	 */
	showModal({
		title = "提示",
		content,
		cancelText = "取消",
		confirmText = "确定",
		success
	} = {}) {
		uni.showModal({
			title: title,
			content: content,
			cancelText: cancelText,
			confirmText: confirmText,
			success: (e) => {
				if (success) {
					success(e);
				}
			}
		});
	},
	/**
	 * 消息提示框
	 */
	showToast({
		title = "提示",
		icon = "none",
		duration = 2000
	} = {}) {
		setTimeout(() => {
			uni.showToast({
				title: title,
				icon: icon,
				duration: duration
			});
		}, 200);

	},
	/**
	 * 获取系统信息
	 * @param {Object} className
	 */
	getSystemInfo({h5}={}) {
		return new Promise(function(resolve, reject) {
			let sysInfo = uni.getStorageSync("sysInfo");
			if (sysInfo != null && sysInfo.windowWidth != undefined) {
				if(h5){
					sysInfo.screenHeight-=sysInfo.windowTop;
				}
				resolve(sysInfo);
			} else {
				uni.getSystemInfo({
					success: function(res) {
						//h5平台减去顶部高度
				
						uni.setStorageSync("sysInfo", res);
						if(h5){
							res.screenHeight-=res.windowTop;
						}
						resolve(res)
					}
				});
			}
		});
	},
	/**
	 * 扫二维码
	 */
	scanCode() {
		return new Promise(function(resolve, reject) {
			uni.scanCode({
				success(res) {

					resolve(res.result);

				},
				fail(res) {
					this.$sys.showToast({
						title: '二维码识别失败'
					});
				}
			});
		});
	},
	/**
	 * 获取节点信息
	 */
	getBoundingClientRect(className, e) {
		return new Promise(function(resolve, reject) {
			let query = uni.createSelectorQuery();
			if (e != undefined) {
				query = uni.createSelectorQuery().in(e);
			}
			query.select(className)
				.boundingClientRect(function(rect) {
					resolve(rect);
				})
				.exec();

		})
	},
	/**
	 * 图片预览
	 */
	previewImage(tempFilePaths) {
		uni.previewImage({
			current: 0,
			urls: tempFilePaths,
			loop: true,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: function(data) {
					console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	},
	/**
	 * 获取图片信息
	 */
	getImageInfo(path) {
		return new Promise(function(resolve, reject) {
			uni.getImageInfo({
				src: path,
				success: (image) => {
					resolve(image);
				}
			});
		});
	},
	/**
	 * 创建图片到本地
	 */
	saveImageToPhotosAlbum(path) {
		return new Promise((resolve, reject) => {


			uni.getSetting({
				success(res) {

					if (res.authSetting['scope.writePhotosAlbum']) {
						downlod(path);

					} else if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success(e) {


								downlod(path);
							},
							fail(e) {
								resolve(false);
							}
						});
					} else {
						uni.openSetting({
							success(res) {
								if (res.authSetting['scope.writePhotosAlbum']) {
									downlod(path);
								}
							}
						});
					}
				}
			});
			var downlod = path => {
				if(path.indexOf('http')==-1){
					uni.saveImageToPhotosAlbum({
						filePath:path,
						success: () => {
							uni.showToast({
								title: "已保存"
							});
							resolve(true);
						},
						fail: () => {
							uni.showToast({
										icon:"none",
								title: "取消保存"
							});
							resolve(false);
						}
					});
				}else{
					this.downloadFile(path).then(res => {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									title: "图片已保存至相册"
								});
								resolve(true);
							},
							fail: () => {
								uni.showToast({
									icon:"none",
									title: "取消保存"
								});
								resolve(false);
							}
						});
					});
				}
				

			};
		});
	},
	/**
	 * 下载图片到本地
	 * @param {Object} path
	 */
	downloadFile(path) {
		return new Promise((resolve, reject)=> {
			uni.downloadFile({
				url: path,
				success: (res) => {
					if (res.statusCode === 200) {
						resolve(res);
					} else {
						this.showToast({
							title: "出错了，请稍候再试"
						})
					}
				},
				fail: (res) => {
					this.showToast({
						title: "出错了，请稍候再试"
					})
					console.log("出错信息：" + JSON.stringify(res));
				}
			});
		});
	},
	/**
	 * 设置复制内容
	 */
	setClipboardData(data) {
		uni.setClipboardData({
			data: data,
			success: function() {

			}
		});
	},
	/**
	 * 修改页面标题
	 * @param {Object} title
	 */
	setNavigationBarTitle(title) {
		uni.setNavigationBarTitle({
			title: title
		});
	},
	/**
	 * 获取设备终端
	 */
	getTerminal() {
		// #ifdef MP-WEIXIN
		return {
			"id": 1002,
			'name': 'MP-WEIXIN',
		};
		// #endif


		// #ifdef H5
		return {
			"id": 2022,
			'name': 'H5',
		};
		// #endif
		// #ifdef MP-ALIPAY
		return {
			"id": 3002,
			'name': 'MP-ALIPAY',
		};
		// #endif
	}
}
